package com.hanthink.web.controller.app.logistics;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.convert.Convert;
import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.domain.entity.SysUser;
import com.hanthink.system.domain.manhour.FactWorkerUsed;
import com.hanthink.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.system.domain.logistics.ProductionDeliveryPlan;
import com.hanthink.system.service.logistics.IProductionDeliveryPlanService;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.common.core.page.TableDataInfo;

/**
 * ProductionDeliveryPlanController
 * 
 * @author lizhao
 * @date 2022-09-05
 */
@RestController
@RequestMapping("/app/logistics/ProductionDeliveryPlan")
public class ProductionDeliveryPlanController extends BaseController
{
    @Autowired
    private IProductionDeliveryPlanService productionDeliveryPlanService;

    @Autowired
    private ISysUserService userService;
    /**
     * 查询ProductionDeliveryPlan列表
     */
    @PreAuthorize("@ss.hasPermi('logistics:ProductionDeliveryPlan:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductionDeliveryPlan productionDeliveryPlan)
    {
        startPage();
        List<ProductionDeliveryPlan> list = productionDeliveryPlanService.selectProductionDeliveryPlanList(productionDeliveryPlan);
        return getDataTable(list);
    }

    /**
     * 导出ProductionDeliveryPlan列表
     */
    @PreAuthorize("@ss.hasPermi('logistics:ProductionDeliveryPlan:export')")
    @Log(title = "ProductionDeliveryPlan", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductionDeliveryPlan productionDeliveryPlan)
    {
        List<ProductionDeliveryPlan> list = productionDeliveryPlanService.selectProductionDeliveryPlanList(productionDeliveryPlan);
        ExcelUtil<ProductionDeliveryPlan> util = new ExcelUtil<ProductionDeliveryPlan>(ProductionDeliveryPlan.class);
        return util.exportExcel(list, "ProductionDeliveryPlan");
    }

    /**
     * 获取ProductionDeliveryPlan详细信息
     */
    @PreAuthorize("@ss.hasPermi('logistics:ProductionDeliveryPlan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(productionDeliveryPlanService.selectProductionDeliveryPlanById(id));
    }

    /**
     * 新增ProductionDeliveryPlan
     */
    @PreAuthorize("@ss.hasPermi('logistics:ProductionDeliveryPlan:add')")
    @Log(title = "ProductionDeliveryPlan", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionDeliveryPlan productionDeliveryPlan)
    {
        return toAjax(productionDeliveryPlanService.insertProductionDeliveryPlan(productionDeliveryPlan));
    }

    /**
     * 修改ProductionDeliveryPlan
     */
    @PreAuthorize("@ss.hasPermi('logistics:ProductionDeliveryPlan:edit')")
    @Log(title = "ProductionDeliveryPlan", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductionDeliveryPlan productionDeliveryPlan)
    {
        return toAjax(productionDeliveryPlanService.updateProductionDeliveryPlan(productionDeliveryPlan));
    }

    /**
     * 删除ProductionDeliveryPlan
     */
    @PreAuthorize("@ss.hasPermi('logistics:ProductionDeliveryPlan:remove')")
    @Log(title = "ProductionDeliveryPlan", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productionDeliveryPlanService.deleteProductionDeliveryPlanByIds(ids));
    }

    @PostMapping("/selectbyid")
    public TableDataInfo selectbyid(@RequestBody Map<String, Object> map){
        ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
        List<ProductionDeliveryPlan> listproductionDeliveryPlan= new ArrayList<>();
        productionDeliveryPlan=productionDeliveryPlanService.selectProductionDeliveryPlanById (Convert.toLong(map.get("id").toString()));
        listproductionDeliveryPlan.add(productionDeliveryPlan);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(listproductionDeliveryPlan);
        return tableDataInfo;
    }
    /**
     *  集货确认操作
     * @param map
     * @return
     * @throws ParseException
     */
    @PostMapping("/listshippingconfirmation")
    public TableDataInfo listshippingconfirmation(@RequestBody Map<String, Object> map) throws ParseException{

        ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
        //工厂代码
        productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());

        List<ProductionDeliveryPlan> listproductionDeliveryPlan = productionDeliveryPlanService.selectshippingconfirmation(productionDeliveryPlan);

        for (ProductionDeliveryPlan item : listproductionDeliveryPlan) {
            //替换确认人名称
            SysUser sysUser= userService.selectUserById(Convert.toLong(item.getCollectionoperator()));
            item.setCollectionoperator(sysUser.getUserName());
        }

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(listproductionDeliveryPlan);
        return tableDataInfo;
    }

    @PostMapping("/listshippingconfirmationnook")
    public TableDataInfo listshippingconfirmationnook(@RequestBody Map<String, Object> map) throws ParseException{

        ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
        //工厂代码
        productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());

        List<ProductionDeliveryPlan> listproductionDeliveryPlan = productionDeliveryPlanService.selectshippingconfirmationnook(productionDeliveryPlan);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(listproductionDeliveryPlan);
        return tableDataInfo;
    }

    @PostMapping("/sureshippingconfirmation")
    public TableDataInfo sureshippingconfirmation(@RequestBody Map<String, Object> map) throws ParseException {

        ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
        //工厂代码
        productionDeliveryPlan.setId(Convert.toLong(map.get("id").toString()));
        //集货确认操作人
        productionDeliveryPlan.setCollectionoperator(map.get("collectionoperator").toString());
        productionDeliveryPlanService.sureshippingconfirmation(productionDeliveryPlan);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        return tableDataInfo;
    }
    /**
     *  车辆到厂确认操作
     * @param map
     * @return
     * @throws ParseException
     */
    @PostMapping("/listdepartureconfirmation")
    public TableDataInfo listdepartureconfirmation(@RequestBody Map<String, Object> map) throws ParseException{

        ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
        //工厂代码
        productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());

        List<ProductionDeliveryPlan> listproductionDeliveryPlan = productionDeliveryPlanService.selectdepartureconfirmation(productionDeliveryPlan);

        for (ProductionDeliveryPlan item : listproductionDeliveryPlan) {
            //替换确认人名称
            SysUser sysUser= userService.selectUserById(Convert.toLong(item.getArriveoperator()));
            item.setArriveoperator(sysUser.getUserName());
        }

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(listproductionDeliveryPlan);
        return tableDataInfo;
    }

    /**
     * 延误处理
     * @param map
     * @return
     * @throws ParseException
     */
    @PostMapping("/delaysinprocessing")
    public TableDataInfo delaysinprocessing(@RequestBody Map<String, Object> map) throws ParseException {

        ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
        //工厂代码
        productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());
        //id
        productionDeliveryPlan.setId(Convert.toLong(map.get("id").toString()));
        //type
        if (map.get("type").toString().equals("1")) {
            productionDeliveryPlan.setCollectioncandelaytime(Convert.toInt(map.get("delaytime").toString()));
            productionDeliveryPlan.setCollectionflag(1);
        } else if (map.get("type").toString().equals("2")) {
            productionDeliveryPlan.setPlanarrivecandelaytime(Convert.toInt(map.get("delaytime").toString()));
            productionDeliveryPlan.setArriveflag(1);
        } else if (map.get("type").toString().equals("3")) {
            productionDeliveryPlan.setBegincandelaytime(Convert.toInt(map.get("delaytime").toString()));
            productionDeliveryPlan.setBeginflag(1);
        }
        productionDeliveryPlanService.updateProductionDeliveryPlan(productionDeliveryPlan);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        return tableDataInfo;
        }

    @PostMapping("/delaysinprocessingsuper")
    public TableDataInfo delaysinprocessingsuper(@RequestBody Map<String, Object> map) throws ParseException {

        ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
        //工厂代码
        productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());
        //id
        productionDeliveryPlan.setId(Convert.toLong(map.get("id").toString()));
        //type
        if (map.get("type").toString().equals("1")) {
            productionDeliveryPlan.setCollectioncandelaytimes(Convert.toInt(map.get("delaytime").toString()));
            productionDeliveryPlan.setCollectionflag(2);
            productionDeliveryPlan.setCollectionmemo(map.get("delayreason").toString());
        } else if (map.get("type").toString().equals("2")) {
            productionDeliveryPlan.setPlanarrivecandelaytimes(Convert.toInt(map.get("delaytime").toString()));
            productionDeliveryPlan.setArriveflag(2);
            productionDeliveryPlan.setArrivememo(map.get("delayreason").toString());
        } else if (map.get("type").toString().equals("3")) {
            productionDeliveryPlan.setBegincandelaytimes(Convert.toInt(map.get("delaytime").toString()));
            productionDeliveryPlan.setBeginflag(2);
            productionDeliveryPlan.setBeginmemo(map.get("delayreason").toString());
        }
        productionDeliveryPlanService.updateProductionDeliveryPlan(productionDeliveryPlan);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        return tableDataInfo;
    }
        @PostMapping("/listdepartureconfirmationnook")
        public TableDataInfo listdepartureconfirmationnook (@RequestBody Map < String, Object > map) throws
        ParseException {

            ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
            //工厂代码
            productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());

            List<ProductionDeliveryPlan> listproductionDeliveryPlan = productionDeliveryPlanService.selectdepartureconfirmationnook(productionDeliveryPlan);

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            tableDataInfo.setRows(listproductionDeliveryPlan);
            return tableDataInfo;
        }

        @PostMapping("/suredepartureconfirmation")
        public TableDataInfo suredepartureconfirmation (@RequestBody Map < String, Object > map) throws ParseException {

            ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
            //工厂代码
            productionDeliveryPlan.setId(Convert.toLong(map.get("id").toString()));
            //车辆到厂确认操作人
            productionDeliveryPlan.setArriveoperator(map.get("arriveoperator").toString());
            productionDeliveryPlanService.suredepartureconfirmation(productionDeliveryPlan);

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            return tableDataInfo;
        }

        /**
         * 车辆出厂确认操作
         * @param map
         * @return
         * @throws ParseException
         */
        @PostMapping("/listvehiclearrivesconfirm")
        public TableDataInfo listvehiclearrivesconfirm (@RequestBody Map < String, Object > map) throws ParseException {

            ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
            //工厂代码
            productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());

            List<ProductionDeliveryPlan> listproductionDeliveryPlan = productionDeliveryPlanService.selectvehiclearrivesconfirm(productionDeliveryPlan);

            for (ProductionDeliveryPlan item : listproductionDeliveryPlan) {
                //替换确认人名称
                SysUser sysUser = userService.selectUserById(Convert.toLong(item.getBeginoperator()));
                item.setBeginoperator(sysUser.getUserName());
            }

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            tableDataInfo.setRows(listproductionDeliveryPlan);
            return tableDataInfo;
        }

        @PostMapping("/listvehiclearrivesconfirmnook")
        public TableDataInfo listvehiclearrivesconfirmnook (@RequestBody Map < String, Object > map) throws
        ParseException {

            ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
            //工厂代码
            productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());

            List<ProductionDeliveryPlan> listproductionDeliveryPlan = productionDeliveryPlanService.selectvehiclearrivesconfirmnook(productionDeliveryPlan);

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            tableDataInfo.setRows(listproductionDeliveryPlan);
            return tableDataInfo;
        }

        @PostMapping("/surevehiclearrivesconfirm")
        public TableDataInfo surevehiclearrivesconfirm (@RequestBody Map < String, Object > map) throws ParseException {

            ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
            //工厂代码
            productionDeliveryPlan.setId(Convert.toLong(map.get("id").toString()));
            //车辆出发确认操作人
            productionDeliveryPlan.setBeginoperator(map.get("beginoperator").toString());

            productionDeliveryPlanService.surevehiclearrivesconfirm(productionDeliveryPlan);

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            return tableDataInfo;
        }

        /**
         * 送货确认操作界面
         * @param map
         * @return
         * @throws ParseException
         */
        @PostMapping("/listarrivalgoodsconfirm")
        public TableDataInfo listarrivalgoodsconfirm (@RequestBody Map < String, Object > map) throws ParseException {

            ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
            //工厂代码
            productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());

            List<ProductionDeliveryPlan> listproductionDeliveryPlan = productionDeliveryPlanService.selectarrivalgoodsconfirm(productionDeliveryPlan);

            for (ProductionDeliveryPlan item : listproductionDeliveryPlan) {
                //替换确认人名称
                SysUser sysUser = userService.selectUserById(Convert.toLong(item.getInpositionoperator()));
                item.setInpositionoperator(sysUser.getUserName());
            }

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            tableDataInfo.setRows(listproductionDeliveryPlan);
            return tableDataInfo;
        }

        @PostMapping("/listarrivalgoodsconfirmnook")
        public TableDataInfo listarrivalgoodsconfirmnook (@RequestBody Map < String, Object > map) throws ParseException
        {

            ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
            //工厂代码
            productionDeliveryPlan.setFactoryid(map.get("factoryid").toString());

            List<ProductionDeliveryPlan> listproductionDeliveryPlan = productionDeliveryPlanService.selectarrivalgoodsconfirmnook(productionDeliveryPlan);

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            tableDataInfo.setRows(listproductionDeliveryPlan);
            return tableDataInfo;
        }

        @PostMapping("/surearrivalgoodsconfirm")
        public TableDataInfo surearrivalgoodsconfirm (@RequestBody Map < String, Object > map) throws ParseException {

            ProductionDeliveryPlan productionDeliveryPlan = new ProductionDeliveryPlan();
            //工厂代码
            productionDeliveryPlan.setId(Convert.toLong(map.get("id").toString()));
            //车辆出发确认操作人
            productionDeliveryPlan.setInpositionoperator(map.get("inpositionoperator").toString());

            productionDeliveryPlanService.surearrivalgoodsconfirm(productionDeliveryPlan);

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("操作成功");
            return tableDataInfo;
        }


}
